(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.wfBpmPkg.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'wfBpmPkgService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, wfBpmPkgService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("wfBpmPkgList");
		ctrl.layoutConfig = layoutConfig;
		wfBpmPkgService.getServiceOptions().then(function(response) {
			ctrl.serviceCodeOptions = response;
		});
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.deployStatusOption = getDeployStatusOptions().data;
			ctrl.search();
		};

		function getDeployStatusOptions() {
			var option = {
					"hasEmptyOption" : true,
					"data" : [{
						"value" : "D",
						"label" : "label.wfbpmpkg.deploystatus.d"
					}, {
						"value" : "U",
						"label" : "label.wfbpmpkg.deploystatus.u"
					} ]
			};
			return option;
		}
		
		ctrl.deploy = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			return authService.deploy(ctrl.selectedRows[0]).then(deploySuccessful, deployFailed);
		};

		function deploySuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.deploy' ],
				'btnCallback' : {
					'close' : function() {
						ctrl.search();
					}
				}
			});
		}

		function deployFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.unDeploy = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			return authService.unDeploy(ctrl.selectedRows[0]).then(unDeploySuccessful, unDeployFailed);
		};

		function unDeploySuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.undeploy' ],
				'btnCallback' : {
					'close' : function() {
						ctrl.search();
					}
				}
			});
		}

		function unDeployFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.setCurrent = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			return authService.setCurrent(ctrl.selectedRows[0]).then(setCurrentSuccessful, setCurrentFailed);
		};

		function setCurrentSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.setcurrent' ],
				'btnCallback' : {
					'close' : function() {
						ctrl.search();
					}
				}
			});
		}

		function setCurrentFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
